<?php defined('_JEXEC') or die('Restricted access'); 

jimport('joomla.application.component.model');

class DocarioModelGroup extends JModel {
    var $_data;
    var $_group;
    var $_id;
    
    var $_query;
    
    function __construct() {
        parent::__construct();
        
        $mainframe = JFactory::getApplication();
        
        $limit = $mainframe->getUserStateFromRequest('global.list.limit','limit',$mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);   
    }
    
    function setId($id) {
        $this->_id = $id;
    }
    
    function getPagination() {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(),
                                                 $this->getState('limitstart'),
                                                 $this->getState('limit'));
        }
        return $this->_pagination;
    }
    
    function getTotal() {
        $this->_buildQuery();
        if (empty($this->_total)) {
            $this->_total = $this->_getListCount($this->_query);
        }
        return $this->_total;
    }
    
    function _buildQuery() {
        $this->_query = 'select * from #__docario_group';
        return $this->_query;
    }
    
    function getData() {
        $this->_buildQuery();
        if (empty($this->_data))
            $this->_data = $this->_getList($this->_query, 
                                           $this->getState('limitstart'),
                                           $this->getState('limit'));
        return $this->_data;
    }
    
    function getGroupList() {
        $this->_buildQuery();
        if (empty($this->_data))
            $this->_data = $this->_getList($this->_query);
            
        return $this->_data;
    }
    
    function getGroupsCategory($category_id) {
        $query = 'select * from #__docario_group a';
        $query .= ' left join (select * from #__docario_category_group_allowed';
        $query .= ' where category_id='.$category_id;
        $query .= ' or category_id is null) b';
        $query .= ' on a.id=b.group_id';
        
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
    
    function _loadGroup() {
        if (empty($this->_group)) {
            $query = 'select * from #__docario_group where id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_group = $this->_db->loadObject();
        }
        
        return (boolean) $this->_group;
    }
    
    function _initGroup() {
        if (empty($this->group)) {
            $group = new stdClass();
            $group->id = 0;
            $group->name = '';
            $group->description = '';
            $this->_group = $group;
            return (boolean) $this->_group;
        }
        return true;
    }
    
    function getGroup() {
        if ($this->_loadGroup()) {
        } else $this->_initGroup();
        return $this->_group;
    }
    
    function deleteGroup($id) {
        $query = 'delete from #__docario_group_member where group_id='.$id;
        $this->_db->setQuery($query);
        $this->_db->query();
    
        $query = 'delete from #__docario_category_group_allowed where group_id='.$id;
        $this->_db->setQuery($query);
        $this->_db->query();
    
        $query = 'delete from #__docario_group where id='.$id;
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function save() {
        $row =& JTable::getInstance('group', 'Table');
    
        $id = JRequest::getVar('id');
        $name = JRequest::getVar('group_name');
        $description = JRequest::getVar('group_description');
        
        if ($id != 0) { $row->set('id', $id); }
        $row->set('name', $name);
        $row->set('description', $description);
        
        if (!$row->check()) {
            $this->setError($this->_db->getErroMsg());
            return false;
        }
        
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        } else {
            // members set
            return true;
        }
    }
    
    function getJUsersCategory($category_id, $search = '') {
        $query = 'select * from #__users a';
        $query .= ' left join (select * from #__docario_category_user_allowed';
        $query .= ' where category_id='.$category_id.' or category_id is null) b';
        $query .= ' on a.id=b.userid';
        $query .= ' where (name like "%'.$search.'%"';
        $query .= ' or username like "%'.$search.'%"';
        $query .= ' or email like "%'.$search.'%")';
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
    
    function getJUsers($search = '') {
        $query = 'select * from #__users';
        $query .= ' where name like "%'.$search.'%"';
        $query .= ' or username like "%'.$search.'%"';
        $query .= ' or email like "%'.$search.'%"';
        
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
    
    function getMembers($group_id) {
        $query = 'select * from #__docario_group_member a';
        $query .= ' inner join #__users b on a.userid=b.id';
        $query .= ' where group_id='.$group_id;
        
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
    
    function addMember($gid, $uid) {
        $query = 'insert into #__docario_group_member (group_id, userid, ';
        $query .= ' administrator) values ('.$gid.', '.$uid.', 0)';
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function removeMember($gid, $uid) {
        $query = 'delete from #__docario_group_member ';
        $query .= ' where group_id='.$gid.' and userid='.$uid;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function adminOn($gid, $uid) {
        $query = 'update #__docario_group_member set administrator=1';
        $query .= ' where group_id='.$gid.' and userid='.$uid;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function adminOff($gid, $uid) {
        $query = 'update #__docario_group_member set administrator=0';
        $query .= ' where group_id='.$gid.' and userid='.$uid;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
}
?>
